package com.company.project.utils;


import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;


public class DateUtils {
    public static String datePattern = "yyyy-MM-dd";
    public static String timeStampPattern = "yyyy-MM-dd HH:mm:ss";
    public static String UnionTimeStampPattern = "YYYYMMddHHmmss";
    public static String YYYYMMDD = "YYYYMMDD";
    public static String UnionDateStampPattern = "MMdd";

    public DateUtils() {
    }

    public static void setDatePattern(String datePattern) {
        if (CommonUtils.isNull(datePattern)) {
            throw new IllegalArgumentException("datePattern can not be blank");
        } else {
            DateUtils.datePattern = datePattern;
        }
    }

    public static void setTimeStampPattern(String timeStampPattern) {
        if (CommonUtils.isNull(datePattern)) {
            throw new IllegalArgumentException("timeStampPattern can not be blank");
        } else {
            DateUtils.timeStampPattern = timeStampPattern;
        }
    }
    /**
     * @description str转date，按照规则转
     * @author sunqiang
     * @date 2019/12/11
     * @param strDate
     * @param regDate
     * @return java.util.Date
     */
    public static Date strToDate(String strDate,String regDate) {
        if(CommonUtils.isNull(strDate)){
            return null;
        }
        SimpleDateFormat formatter = new SimpleDateFormat(regDate);
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }
    public static Date toDate(String dateStr) {
        if (CommonUtils.isNull(datePattern)) {
            return null;
        } else {
            dateStr = dateStr.trim();
            int length = dateStr.length();

            try {
                SimpleDateFormat sdfDate;
                if (length == timeStampPattern.length()) {
                    sdfDate = new SimpleDateFormat(timeStampPattern);

                    try {
                        return sdfDate.parse(dateStr);
                    } catch (ParseException var4) {
                        dateStr = dateStr.replace(".", "-");
                        dateStr = dateStr.replace("/", "-");
                        return sdfDate.parse(dateStr);
                    }
                } else if (length == datePattern.length()) {
                    sdfDate = new SimpleDateFormat(datePattern);

                    try {
                        return sdfDate.parse(dateStr);
                    } catch (ParseException var5) {
                        dateStr = dateStr.replace(".", "-");
                        dateStr = dateStr.replace("/", "-");
                        return sdfDate.parse(dateStr);
                    }
                } else {
                    throw new IllegalArgumentException("The date format is not supported for the time being");
                }
            } catch (ParseException var6) {
                throw new IllegalArgumentException("The date format is not supported for the time being");
            }
        }
    }

    public static String toStr(Date date) {
        return toStr(date, datePattern);
    }

    public static String toStr(Date date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    public static String toStr(Long date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    public static String toStr(Date date, long time, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date.getTime() + time);
    }

    public static long subtract_second(Date date1,Date date2){
        long diff = Math.abs(date2.getTime() - date1.getTime());
        return diff;
    }

//    public static void main(String[] args) {
//        System.out.println(toStr(new Date(), UnionDateStampPattern));
//    }
}
